﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using Microsoft.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;
using Staff = UDS.Components.Staff;

namespace UDS.Inc
{
    /// <summary>
    ///	ControlPositionTreeView 的摘要说明。
    /// </summary>
    [CLSCompliant(false)]
    public partial class ControlPositionTreeView : UserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IList<PositionInfo> positions = UDS.Components.Position.GetAllPositions();
                InitTreeRootNode(positionTreeView.Nodes, positions);

                if (positionTreeView.Nodes.Count > 0)
                {
                    positionTreeView.Nodes[0].Expanded = true;
                }
            }

            base.OnLoad(e);
        }

        /// <summary>
        /// 初始化TreeView 的 RootNode
        /// </summary>
        private void InitTreeRootNode(TreeNodeCollection nodes, IList<PositionInfo> list)
        {
            foreach (var p in list)
            {
                if (p.ID == p.ParentPositionID)
                {
                    var node = new TreeNode();
                    node.ID = p.ID.ToString();
                    node.Text = "<span onmouseover=javascript:title='" + HttpUtility.HtmlAttributeEncode(p.Description) + "'>" + HttpUtility.HtmlEncode(p.Name) + "</span>";
                    node.ImageUrl = GetIcon("8");
                    node.NavigateUrl = "ListView.aspx?PositionID=" + p.ID;
                    node.Target = "MainFrame";
                    nodes.Add(node);

                    InitTreeChildNode(node.Nodes, p.ID, list);
                }
            }
        }

        /// <summary>
        /// 初始化TreeView 的 ChildNode
        /// </summary>
        private void InitTreeChildNode(TreeNodeCollection nodes, int parentId, IList<PositionInfo> list)
        {
            foreach (var p in list)
            {
                if (p.ParentPositionID == parentId && p.ID != p.ParentPositionID)
                {
                    var node = new TreeNode();
                    node.ID = p.ID.ToString();
                    node.Text = "<span onmouseover=javascript:title='" + HttpUtility.HtmlAttributeEncode(p.Description) + "'>" + HttpUtility.HtmlEncode(p.Name) + "</span>";
                    node.ImageUrl = GetIcon("9");
                    node.NavigateUrl = "ListView.aspx?PositionID=" + p.ID;
                    node.Target = "MainFrame";
                    nodes.Add(node);

                    // InitChildNodeDataTable(positionId);
                    InitTreeChildNode(node.Nodes, p.ID, list);
                }
            }
        }

        #region 获取节点图标

        /// <summary>
        /// 获取节点图标
        /// </summary>
        private string GetIcon(string ClassType)
        {
            string rtnValue = "../../DataImages/";
            switch (ClassType)
            {
                case "0":
                    rtnValue += "flag.gif";
                    break;
                case "1":
                    rtnValue += "myDoc.gif";
                    break;
                case "2":
                    rtnValue += "mail.gif";
                    break;
                case "3":
                    rtnValue += "page.gif";
                    break;
                case "4":
                    rtnValue += "scales.gif";
                    break;
                case "5":
                    rtnValue += "mail.gif";
                    break;
                case "6":
                    rtnValue += "help_page.gif";
                    break;
                case "7":
                    rtnValue += "red_ball.gif";
                    break;
                case "8":
                    rtnValue += "search_globe.gif";
                    break;
                case "9":
                    rtnValue += "person.gif";
                    break;
                default:
                    rtnValue += "";
                    break;
            }
            return rtnValue;
        }

        #endregion
    }
}